package com.class06.controller;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@RestController
@RequestMapping("/orders")
public class OrderController {

    private final ConcurrentHashMap<String, String> orderMap = new ConcurrentHashMap<>();

    @PostMapping("/")
    public ResponseEntity<String> createOrder(@RequestBody Map<String, String> payload, @RequestHeader("X-Request-ID") String requestId) {
        // 检查是否重复请求
        if (orderMap.containsKey(requestId)) {
            return ResponseEntity.ok("Order already processed with ID: " + requestId);
        }

        // 处理业务逻辑，创建订单
        String orderId = "ORD" + System.nanoTime();
        orderMap.put(requestId, orderId);

        return ResponseEntity.ok("Order created with ID: " + orderId);
    }
}